package com.xiaoq.manager.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.user.IMerchantMgrApi;
import com.xiaoq.apis.user.MerchantMgrInfo4Vo;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.manager.entity.MerchantMgrEntity;
import com.xiaoq.manager.entity.MerchantRoleEntity;
import com.xiaoq.manager.repository.IMerchantMgrRepository;
import com.xiaoq.manager.service.IMerchantRoleService;
@Service(interfaceClass=IMerchantMgrApi.class,version="1.0.0")
public class MerchantMgrDubboServiceImpl implements IMerchantMgrApi {

	@Autowired
	private IMerchantMgrRepository merMgrRep;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IMerchantRoleService merchantRoleSer;
	
	private static final Logger LOG = LoggerFactory.getLogger(MerchantMgrDubboServiceImpl.class);
	
	
	@Override
	public MerchantMgrInfo4Vo findMerMgr(String managerId) {
		MerchantMgrEntity merchantMgr = merMgrRep.findById(managerId);
		MerchantMgrInfo4Vo merchantMgrInfo = new MerchantMgrInfo4Vo();
		if (merchantMgr != null) {
			BeanUtils.copyProperties(merchantMgr, merchantMgrInfo);
			
		}
		return merchantMgrInfo;
		
	}
	
	@Override
	public MerchantMgrInfo4Vo findByIdByMerchantId(String merchantId) {
		MerchantMgrEntity merchantMgr = merMgrRep.findByIdByMerchantId(merchantId);
		MerchantMgrInfo4Vo merchantMgrInfo = new MerchantMgrInfo4Vo();
		if (merchantMgr != null) {
			BeanUtils.copyProperties(merchantMgr, merchantMgrInfo);
		}
		return merchantMgrInfo;
	}


	@Override
	public boolean save(MerchantMgrInfo4Vo merchantMgrInfo4Vo) {
		String id = merchantMgrInfo4Vo.getId();
		MerchantMgrEntity merchantMgr = merMgrRep.findById(id);
		String merchantId = merchantMgrInfo4Vo.getMerchantId();
		String shopId = merchantMgrInfo4Vo.getShopId();
		String shopType = merchantMgrInfo4Vo.getShopType();
		String logo = merchantMgrInfo4Vo.getLogo();
		merchantMgr.setMerchantId(merchantId);
		merchantMgr.setShopId(shopId);
		merchantMgr.setShopType(shopType);
		merchantMgr.setLogo(logo);
		MerchantMgrEntity merchantMgrInDB = merMgrRep.save(merchantMgr);
		if(merchantMgrInDB!=null){
			return true;
		}
		return false;
	}


	@Override
	public boolean updateStep(String userId,int step) {
		MerchantMgrEntity merchantMgr = merMgrRep.findById(userId);
		merchantMgr.setStep(step);
		merMgrRep.save(merchantMgr);
		return true;
	}


	@Override
	public void addRole(String shopId) {
		MerchantRoleEntity merchantRole = new MerchantRoleEntity();
		//添加一个运营
		merchantRole.setId(idGenerator.nextId());
		merchantRole.setCode("100");
		merchantRole.setSitesId(shopId);
		merchantRole.setName("运营");
		merchantRoleSer.add(merchantRole);
		// 添加一个编辑
		merchantRole.setId(idGenerator.nextId());
		merchantRole.setCode("100");
		merchantRole.setSitesId(shopId);
		merchantRole.setName("编辑");
		merchantRoleSer.add(merchantRole);
		
	}


}
